from fastapi import APIRouter, Query,UploadFile,Request
from tortoise.expressions import Q
from app.controllers.appointment.designer import appointment_designer_controller
from app.controllers.appointment.space import appointment_space_controller
from app.controllers.system.user import user_controller
from app.core.ctx import CTX_USER_ID
from app.schemas.base import Success, SuccessExtra,Fail
from app.schemas.appointment import *
from app.settings import settings
from app.utils.upload import upload_image,upload_file

appointment_designer_router = APIRouter()
model_name = "预约对象"



@appointment_designer_router.get("/list", summary=f"查看{model_name}列表")
async def do_list(
        page: int = Query(1, description="页码"),
        page_size: int = Query(10, description="每页数量"),
        name: str = Query(None, description=f"{model_name}名称"),
        space_id: int = Query(0, description="门店id"),
):
    q = Q()
    if name:
        q &= Q(name__contains=name)
    user_id = CTX_USER_ID.get()
    user = await user_controller.get(user_id)
    if space_id:
        q &= Q(space_id__contains=[space_id])
    if user.space_id and space_id == 0:
        for space in user.space_id:
            q = q | Q(space_id__contains=[space])
    total, data = await appointment_designer_controller.do_list(page=page, page_size=page_size, search=q)
    for item in data:
        item["label"] = item['name']
        item["value"] = item['id']
        if item["space_id"]:
            item["space_name"] = await appointment_space_controller.get_space_names(item["space_id"])
    return SuccessExtra(data=data, total=total, page=page, page_size=page_size)

@appointment_designer_router.get("/get", summary=f"查看{model_name}")
async def do_get(
        data_id: int = Query(..., description="ID"),
):
    data = await appointment_designer_controller.do_detail(data_id=data_id)
    return Success(data=data)

@appointment_designer_router.post("/create", summary=f"创建{model_name}")
async def do_create(
        request: Request,
        data_in: AppointmentDesignerCreate,
):
    await appointment_designer_controller.do_create(request,obj_in=data_in)
    return Success(msg="创建成功")

@appointment_designer_router.post("/update", summary=f"更新{model_name}")
async def do_update(
        request: Request,
        data_in: AppointmentDesignerUpdate,
):
    await appointment_designer_controller.do_update(request,obj_in=data_in)
    return Success(msg="更新成功")

@appointment_designer_router.delete("/delete", summary=f"删除{model_name}")
async def do_delete(
        request: Request,
        data_id: int = Query(..., description="ID"),
):
    await appointment_designer_controller.do_delete(request,data_id=data_id)
    return Success(msg="删除成功")

@appointment_designer_router.post("/file", summary="上传图片")
async def do_file(file: UploadFile):
    url = await upload_image(file,"appointment/designer")
    if not url:
        return Fail(code=400, msg="图片格式错误")
    data = {"url": url}
    return Success(data=data)


@appointment_designer_router.get("/doctor_type", summary="获取医生类型")
async def doctor_type():
    level_objs = settings.APPOINTMENT_DOCTOR_TYPE
    data = []
    key = 0
    for level in level_objs:
        if level:
            data.append({"label":level,"value":key})
        key += 1
    return Success(data=data)

@appointment_designer_router.get("/designer_type", summary="获取预约角色类型")
async def designer_type():
    level_objs = settings.APPOINTMENT_DESIGNER_TYPE
    data = []
    key = 0
    for level in level_objs:
        if level:
            data.append({"label":level,"value":key})
        key += 1
    return Success(data=data)